﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using CyzFaceBLL.BLL;
using CyzFaceBLL.Models;
using CyzFaceBLL.Common;
using CyzFaceBLL.Global;
using CyzFaceBLL.Helper;

namespace FaceDetectionDemo
{
    public partial class Form2 : Form
    {
        private Deal dealObj = Deal.Instance;
        private MQTTBrokerHelper mQTTBrokerHelper = MQTTBrokerHelper.Instance;
        private MQTTHelper mQTTHelper = MQTTHelper.Instance;

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            try
            {
                GlobalParameters.Init();
                this.mQTTBrokerHelper.Start();

                Task.Run(() =>
                {
                    try
                    {
                        this.mQTTHelper.Connect();
                    }
                    catch (Exception ex)
                    {
                        LogWrite.TxtExport_DateNow("LogErr.txt", "Form2_Load 0Err:" + ex.Message);
                    }
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show("Form2_Load Err:" + ex.Message);
            }
        }

        private void btnSelectPath1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog fileDialog = new OpenFileDialog();
                fileDialog.Multiselect = true;
                fileDialog.Title = "请选择文件";
                //fileDialog.Filter = "所有文件(*xls*)|*.xls*"; //设置要选择的文件的类型
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    string file = fileDialog.FileName;//返回文件的完整路径 
                    this.txtFilePath1.Text = file;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Err:" + ex.Message);
            }
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (MessageBox.Show("是否要退出本程序？", "退出程序", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    this.mQTTBrokerHelper.End();
                    this.mQTTHelper.DisConnect();
                    System.Diagnostics.Process.GetCurrentProcess().Kill();
                }
                else
                {
                    e.Cancel = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Err:" + ex.Message);
            }
        }

        private void btnFaceDetect_Click(object sender, EventArgs e)
        {
            try
            {
                this.dealObj.FaceImgPathFile = this.txtFilePath1.Text;
                var dtStart = DateTime.Now;
                var v = this.dealObj.FaceDetect();
                var dtEnd = DateTime.Now;

                this.pic1.Image = v.OriginalImg;
                this.pic2.Image = v.FinishImg;
                this.lblFaceDetectMsg.Text = DateTime.Now.ToString("HH:mm:ss.fff") + " 此次人脸检测花费了 " + Tool.DiffMilliseconds(dtStart, dtEnd).ToString() + " 毫秒.";

                ////日志
                //string strLog = "\r\n【测试数据】 = \r\n" + Tool.ConvertJsonString(Newtonsoft.Json.JsonConvert.SerializeObject(v)) + "\r\n";
                //LogWrite.TxtExport_DateNow("LogTest.txt", strLog);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Err:" + ex.Message);
            }
        }

        private void btnFileSelect2_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog fileDialog = new OpenFileDialog();
                fileDialog.Multiselect = true;
                fileDialog.Title = "请选择文件";
                //fileDialog.Filter = "所有文件(*xls*)|*.xls*"; //设置要选择的文件的类型
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    string file = fileDialog.FileName;//返回文件的完整路径 
                    this.txtFilePath2.Text = file;
                    this.pic3.Image = Image.FromFile(file);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Err:" + ex.Message);
            }

        }

        private void btnSelectFile3_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog fileDialog = new OpenFileDialog();
                fileDialog.Multiselect = true;
                fileDialog.Title = "请选择文件";
                //fileDialog.Filter = "所有文件(*xls*)|*.xls*"; //设置要选择的文件的类型
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    string file = fileDialog.FileName;//返回文件的完整路径 
                    this.txtFilePath3.Text = file;
                    this.pic4.Image = Image.FromFile(file);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Err:" + ex.Message);
            }
        }

        private void btnFaceCompare_Click(object sender, EventArgs e)
        {
            try
            {
                this.dealObj.FaceImgPathFile = this.txtFilePath2.Text;
                var v1 = this.dealObj.FaceDetect();

                this.dealObj.FaceImgPathFile = this.txtFilePath3.Text;
                var v2 = this.dealObj.FaceDetect();

                CompareResultModel compareResultModel = new CompareResultModel()
                {
                    SourceModel = v1.ResultSingleModelList[0],
                    TargetModel = v2.ResultSingleModelList[0]
                };
                compareResultModel = this.dealObj.FaceCompare(compareResultModel);

                this.LblResultMsg.Text = "两张人脸的相似度为: " + compareResultModel.Similarity.ToString() + "\r\n\r\n";
                this.LblResultMsg.Text += (compareResultModel.IsSelf ? " 为同一个人!" : " 不是同一个人!");

                if (compareResultModel.IsSelf)
                {
                    var authorizeResultModel = this.dealObj.FaceAuthorize(compareResultModel.SourceModel);
                    string sJson = Newtonsoft.Json.JsonConvert.SerializeObject(authorizeResultModel);
                    this.mQTTHelper.Publish("TestFaceAuthorize", sJson);

                    string strLog = "\r\n【测试数据】 = \r\n" + Tool.ConvertJsonString(sJson) + "\r\n";
                    LogWrite.TxtExport_DateNow("LogTest.txt", strLog);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Err:" + ex.Message);
            }
        }
    }
}
